﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;

using System.Data.SqlClient;

namespace BenhVien
{
    public partial class fmDangNhap : Form
    {
        static int check=0;
        private Connection cons = new Connection();
        static DataTable dt = new DataTable();
        private DataTable dtQuyen = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();
        static String UserName = null;
        static String Pass = null;
        public fmDangNhap()
        {
            InitializeComponent();
        }

        private void btLogin_Click(object sender, EventArgs e)
        {
            if (check >= 3)
            {
                MessageBox.Show("Bạn đã đăng nhập thất bại quá số lần cho phép! Vui lòng chờ!", "Đăng Nhập", MessageBoxButtons.OK);
                Thread.Sleep(5000);
               // Application.DoEvents();
                check = 0;
                MessageBox.Show("Mời bạn tiếp tục!", "Đăng Nhập", MessageBoxButtons.OK);
            }
            else
            {
                if (tbUserName.Text == "admin")
                {
                    SqlCommand commandAdmin = new SqlCommand();
                    commandAdmin.Connection = cons.con;
                    commandAdmin.CommandType = CommandType.Text;
                    commandAdmin.CommandText = "Select Name, Pass From DangNhap Where Name='admin'";
                    da.SelectCommand = commandAdmin;
                    da.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0][1].ToString() == tbPass.Text)
                        {
                            UserName = tbUserName.Text;
                            Pass = tbPass.Text;
                            Hide();

                            FormChinh _fmMain = new FormChinh();

                            _fmMain.Show();
                        }
                        else
                        {
                            check++;
                            if (MessageBox.Show("Password của Admin sai. Admin có muốn nhập lại?", "Đăng nhập", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            {
                                Close();
                                Dispose();
                                cons.DisConnect();
                                System.Windows.Forms.Application.Exit();
                            }
                        }
                    }
                }
                else
                {
                    SqlCommand commandLogin = new SqlCommand();
                    commandLogin.Connection = cons.con;
                    commandLogin.CommandType = CommandType.Text;
                    commandLogin.CommandText = @"Select DangNhap.Name as N'Name',DangNhap.Pass as N'Pass', NhanVien.Vitri as N'Vitri'
                                           From DangNhap,NhanVien
                                            Where NhanVien.MaNV=DangNhap.MaNV and Name = @Name and Pass = @Pass";
                    commandLogin.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = tbUserName.Text;
                    commandLogin.Parameters.Add("@Pass", SqlDbType.NVarChar, 50).Value = tbPass.Text;
                    //commandLogin.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                    da.SelectCommand = commandLogin;
                    da.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        UserName = tbUserName.Text;
                        Pass = tbPass.Text;

                        /* if (dt.Rows[0][2].ToString() == "Y Tá")
                         {

                             _fmNhanVien.btAdd.Enabled = false;
                             _fmNhanVien.Show();

                         }
                         else if (dt.Rows[0][2].ToString() == "Bác Sĩ")
                         {
                             _fmNhanVien.btEdit.Enabled = false;
                             _fmNhanVien.Show();
                         }*/
                        Hide();

                        FormChinh _fmMain = new FormChinh();

                        _fmMain.Show();


                    }
                    else
                    {
                        check++;
                        if (MessageBox.Show("Username hoặc Pass đã sai. Bạn có muốn đăng nhập lại", "Đăng nhập", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            Close();
                            Dispose();
                            System.Windows.Forms.Application.Exit();
                        }
                    }
                }
            }
            
                
            
        }
        public static String getUserName()
        {
            return UserName;
        }
        public static String getPass()
        {
            return Pass;
        }
        public static void setPass(String s)
        {
            Pass = s;
        }
        public static DataTable getDataTable()
        {
            return dt;
        }
        private void fmDangNhap_Load(object sender, EventArgs e)
        {
            check = 0;
            this.AcceptButton = btLogin;
            cons.Connect();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            System.Windows.Forms.Application.Exit();
        }
       
    }
}
